<?php
namespace Admin\Controller;

use Common\Controller\AdminbaseController;
class VarController extends AdminbaseController{
	

	
	function _initialize() {
		parent::_initialize();
		$this->db = D("Var");
		$this->cate = D("VarCat");
		
	}
	
	function index(){
	
		$categorys=$this->cate->order("id desc")->select();
		
		$this->assign("list",$categorys);
		
		
		$this->display();
	}
	
	/**
	 *  添加分类
	 */
	public function add_cate() {
		if (IS_POST) {
			if ($this->cate->create()) {
				if ($this->cate->add()!==false) {
					$this->success("添加成功！", U("index"));
				} else {
					$this->error("添加失败！");
				}
			} else {
				$this->error($this->cate->getError());
			}
		}else{
		
		$this->display("cate");
		}
	}
	
	function edit_cate(){

		if (IS_POST) {
			if ($this->cate->create()) {
				if ($this->cate->save()!==false) {
					$this->success("保存成功！", U("index"));
				} else {
					$this->error("保存失败！");
				}
			} else {
				$this->error($this->cate->getError());
			}
		}
		else{
		$id= intval(I("get.id"));
		$cate=$this->cate->where("id=$id")->find();
		$this->assign("data",$cate);
		$this->assign("edit",true);
		$this->display("cate");
		}
	}
	
	function db_list(){
		$catid=I("get.catid");
		if(!$catid){
			$this->error("缺少参数！");
		}
		$result=$this->db->where(array("catid"=>$catid))->order(array("listorder" => "DESC","id"=>"ASC"))->select();
		
		
		
		
		import ( "Tree" );
		$tree = new \Tree ();
		$tree->icon = array (
				'&nbsp;&nbsp;&nbsp;│ ',
				'&nbsp;&nbsp;&nbsp;├─ ',
				'&nbsp;&nbsp;&nbsp;└─ '
		);
		$tree->nbsp = '&nbsp;&nbsp;&nbsp;';
		
		$newmenus = array ();
		foreach ( $result as $m ) {
			$newmenus [$m ['id']] = $m;
		}
		foreach ( $result as $n => $r ) {
				
			$result [$n] ['level'] = $this->_get_level ( $r ['id'], $newmenus );
			$result [$n] ['parentid_node'] = ($r ['parentid']) ? ' class="child-of-node-' . $r ['parentid'] . '"' : '';

			if(UID==1){
				$result [$n] ['str_manage'] = ' <a data-toggle="tooltip" title="修改" class="btn btn-xs btn-info tooltip-info" href="' . U("edit", array("id" => $r['id'])) . '"><i class="icon-edit" ></i></a> <a data-toggle="tooltip" title="删除"  class="ajax-del btn btn-xs btn-danger	tooltip-danger"  href="' . U("delete", array("id" => $r['id']) ). '"><i class="icon-trash" ></i></a>';
					
			}
			else{
				$result [$n] ['str_manage'] = ' <a data-toggle="tooltip" title="修改" class="btn btn-xs btn-info tooltip-info" href="' . U("edit", array("id" => $r['id'])) . '"><i class="icon-edit" ></i></a>';
					
			}
				
			
			
			$result [$n] ['status'] = $r ['status'] ? '<a href="' . U ( "change", array (
					"field" => "status",
					"value" => "0",
					"id" => $r [id]
			) ) . '" class="ajax-switch"><i class="icon-check-circle"></i></a>' : '<a href="' . U ( "change", array (
					"field" => "status",
					"value" => "1",
					"id" => $r [id]
			) ) . '" class="ajax-switch"><i class="icon-times-circle"></i></a>';
			
			$result [$n] ['default'] = $r ['default'] ? '<a href="' . U ( "changedefault", array (
					
					"id" => $r [id]
			) ) . '" class="ajax-get"><i class="icon-check-circle"></i></a>' : '<a href="' . U ( "changedefault", array (
				
					"id" => $r [id]
			) ) . '" class="ajax-get"><i class="icon-times-circle"></i></a>';
		
				
		}
		
		$tree->init ( $result );
		$str = "<tr id='node-\$id' \$parentid_node>
					<td><input name='listorders[\$id]' type='text' size='4' value='\$listorder' class='input input-order'></td>
					<td class='hidden-480'>\$id</td>
        			<td>\$spacer\$name</td>
					
        	
        		<td>\$status</td>
				<td>\$default</td>
		
		
					<td>\$str_manage</td>
				</tr>";
		$categorys = $tree->get_tree ( 0, $str );
		$this->assign ( "list", $categorys );
		
		//print_r($categorys);
		
		
		
		
		
		$cate_name=$this->cate->where(array("id"=>$catid))->getField("name");
		$this->assign("catname",$cate_name);
		$this->display();
		
		
		
		
	}
	
	function add(){
		
		if(IS_POST){
			cookie("add_var_cate",$_POST[parentid]);
			
			$rs=$this->db->where(array("name"=>$_POST['name'],"catid"=>$_POST['catid']))->find();
			if($rs)$this->error("该记录重复！");
			
			if ($this->db->create()) {
				if ($this->db->add()!==false) {
					$this->success("添加成功！");
				} else {
					$this->error("添加失败！");
				}
			} else {
				$this->error($this->db->getError());
			}
		}else{
	
			
			
			$cate=$this->cate->where(array("id"=>I("get.catid")))->find();
		
		$this->assign ( "select_categorys", $this->db->get_admin_tree (I("get.catid"),cookie("add_var_cate")) );
	
		$this->assign("cate",$cate);
		$this->display("form");
		}
		}
	
	
	function edit(){
		
		
		if(IS_POST){
			
			$rs=$this->db->where(array("name"=>$_POST['name'],"catid"=>$_POST['catid']))->find();
			if($rs&&$rs[id]!=$_POST[id])$this->error("该记录重复！");
			
			
			if ($this->db->create()) {
				
				if ($this->db->save()!==false) {
					$this->success("保存成功！", U("db_list",array("catid"=>$_POST['catid'])));
				} else {
					$this->error("保存失败！");
				}
			} else {
				$this->error($this->db->getError());
			}
		
		}else{
	
		$id= intval(I("get.id"));
		if(!$id){
			$this->error("缺少参数！");
		}
	
		
		$r=$this->db->where("id=$id")->find();
		$this->assign("edit",true);
		$this->assign("data",$r);
		$this->assign ( "select_categorys", $this->db->get_admin_tree ($r[catid],$r[parentid]) );
	
		
		$cate=$this->cate->where(array("id"=>$r['catid']))->find();
		
		
		$this->assign("cate",$cate);
		
		
		$this->display("form");
		}
	}
	

	
	

	function delete(){
		if(isset($_GET['id'])){
			$id = intval(I("get.id"));
	
			$rs=$this->db->where(array("id"=>$id))->getField("thumb");
			if ($this->db->delete($id)) {
				
				D('File')->del_file($rs);
				
				$this->success("删除成功！");
			} else {
				$this->error("删除失败！");
			}
		}
		if(isset($_POST['ids'])){
			$ids=I("param.ids");
	
			$map[id]=array("in",$ids);
	
			if ($this->db->where($map)->delete()) {
				$this->success("删除成功！");
			} else {
				$this->error("删除失败！");
			}
		}
	}
	
	function delete_cate(){
	
			$id = intval(I("get.id"));
			
			if(!$id){
				$this->error("缺少参数！");
			}
	
			$count=$this->db->where(array("catid"=>$id))->find();

			if($count){
				$this->error("该栏目下还有数据，删除失败");
			}
			
			
			if ($this->cate->delete($id)) {
	
	
				$this->success("删除成功！");
			} else {
				$this->error("删除失败！");
			}
		
	}
	
	
	
	//排序
	public function listorders() {
		$status = parent::_listorders($this->db);
		if ($status) {
			$this->success("排序更新成功！");
		} else {
			$this->error("排序更新失败！");
		}
	}
	
	/**
	 * 改变默认值
	 */
	public function changedefault() {
		$id = $_REQUEST[id];
		
		
		
		$r = $this->db->where(array("id" => $id))->find();
		
		$status = $r['default'] ? 0 : 1;
			
		
	
		if ($r['catid']) {
			
		
			$result1=$this->db->query("update `__PREFIX__var` SET `default`=0 WHERE `catid` = '$r[catid]'");
			$result=$this->db->save(array("default"=>$status,"id"=>$id));
	
			if ($result1 !== false&&$result !== false) {
	
				$this->success("转换成功！");
	
			} else {
			$this->error("转换失败！");
			}
		} else {
			$this->error("不存在！");
		}
	}
	
	
	
	protected function _get_cate_tree($value){
		
		$list=$this->cate->select();

		if($value){
			foreach ($list as $key=>$v){
				
				if($value==$v[id]) {
					$list[$key]['selected']="selected";
					
				}
				
			}
			
		}
		return $list;
		
	}
	
	/**
	 * 获取菜单深度
	 *
	 * @param
	 *        	$id
	 * @param
	 *        	$array
	 * @param
	 *        	$i
	 */
	protected function _get_level($id, $array = array(), $i = 0) {
		if ($array [$id] ['parentid'] == 0 || empty ( $array [$array [$id] ['parentid']] ) || $array [$id] ['parentid'] == $id) {
			return $i;
		} else {
			$i ++;
			return $this->_get_level ( $array [$id] ['parentid'], $array, $i );
		}
	}
	
	
	
	}
